function smooth = GaussianSmooth(data, stddev)
sigma = stddev^2;

if ~exist('data'); data = rand(1000,1); end;
if ~exist('stddev'); stddev = 10; end;

% Setup sliding compensation: Sum of gauss should be 1 so the psf must be
% wide enough.
sizeOfSlidingwindow = 6*stddev; % >99%

% Create a Point-Spread function of the Gaussian pdf
gmd = gmdistribution(0, sigma);
psf = pdf(gmd, linspace(-sizeOfSlidingwindow/2, sizeOfSlidingwindow/2, sizeOfSlidingwindow)');
% Stretch the psf so mean of data won't change
% sum(psf)
psf = psf * (1/sum(psf));
% sum(psf)

% Make sure there are no edge side effects
safedata = [ones(sizeOfSlidingwindow/2,1)*data(1);data;ones(sizeOfSlidingwindow/2,1)*data(end)];

% Convolve the orignal data with the Point-Spread function to smooth
cdata = conv(safedata, psf);
% Cut off the safe edges and the convolution edges
smooth = cdata(sizeOfSlidingwindow+1:length(data)+sizeOfSlidingwindow);
% NOTE: beginning and end of data should also be trimmed to throw away the
% measurements influenced by only part of the pdf (e.g. first and last
% 100, depending on the shape of the pdf)

% figure;
% H1 = subplot(3,1,1);
% plot(data);
% hold on;
% plot(smooth, 'r');
% title 'TFC Data'
% xl = get(H1, 'xlim');
% yl = get(H1, 'ylim');
% ylim(yl);
% legend('data', 'smoothed data');
% 
% subplot(3,1,2);
% ezplot(@(x)pdf(gmd,x), [-sizeOfSlidingwindow/2 sizeOfSlidingwindow/2]);
% ylim('auto');
% title(['Gaussian pdf (stddev: ' num2str(sqrt(sigma)) ')']);
% 
% subplot(3,1,3);
% plot(smooth);
% title 'Gaussian smoothed TFC Data...'
% xlim(xl);
% ylim(yl);

end